package com.yindian.cos.easy.security.authentication.token;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.yindian.cos.easy.security.authentication.login.UserDetail;
import com.yindian.cos.easy.security.exception.BuilderTokenException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 登录成功后颁发token
 *
 * @author zhangchuanhao
 * @create 2018/11/21
 */
@Data
@NoArgsConstructor
public class Token implements Serializable {

    /**
     * token
     */
    @JsonProperty("access_token")
    private String accessToken;

    /**
     * 刷新token
     */
    @JsonProperty("refresh_token")
    private String refreshToken;

    /**
     * token过期时间（秒）
     */
    @JsonProperty("access_token_expire")
    private int accessTokenExpire;

    /**
     * refreshToken过期时间
     */
    @JsonProperty("refresh_token_expire")
    private int refreshTokenExpire;

    /**
     * 业务系统的用户唯一标识
     */
    @JsonProperty("user_id")
    private String userId;

    /**
     * 登录用户的信息，由用户自己实现
     */
    @JsonProperty("user_detail")
    private UserDetail userDetail;

    /**
     * redis tokenKey
     */
    @JsonProperty("token_key")
    private String tokenKey;

    /**
     * 创建时间
     */
    @JsonProperty("create_time")
    private Date createTime;

    public Token(String accessToken,
                 int accessTokenExpire,
                 String refreshToken,
                 int refreshTokenExpire,
                 String userId,
                 UserDetail userDetail,
                 String tokenKey) {
        if (accessTokenExpire >= refreshTokenExpire || refreshTokenExpire == 0 || accessTokenExpire == 0) {
            throw new BuilderTokenException("access token expire和refresh token expire必须大于0并且refresh token expire必须大于等于access token expire");
        }
        if (StringUtils.isBlank(accessToken)) {
            throw new BuilderTokenException("access token不能为空");
        }
        if (StringUtils.isBlank(refreshToken)) {
            throw new BuilderTokenException("refresh token不能为空");
        }
        if (StringUtils.isBlank(userId)) {
            throw new BuilderTokenException("userId不能为空");
        }
        if (StringUtils.isBlank(tokenKey)) {
            throw new BuilderTokenException("tokenKey不能为空");
        }
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        this.accessTokenExpire = accessTokenExpire;
        this.refreshTokenExpire = refreshTokenExpire;
        this.userId = userId;
        this.userDetail = userDetail;
        this.tokenKey = tokenKey;
        this.createTime = new Date();
    }

    /**
     * 刷新token过期时间
     */
    public static void refreshExpire(Token token) {
        // 当前时间 - 创建时间 = token已使用时间
        int timeDifference = (int) (System.currentTimeMillis() - token.createTime.getTime());
        timeDifference = timeDifference / 1000;
        // accessToken剩余过期时间
        token.accessTokenExpire = token.accessTokenExpire - timeDifference;
        // refreshToken剩余过期时间
        token.refreshTokenExpire = token.refreshTokenExpire - timeDifference;
    }
}
